﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BG_Manage_BG_Admin : System.Web.UI.Page
{
    VPINews.BLL.Admin bll_admin = new VPINews.BLL.Admin();
    VPINews.Model.Admin mod_admin = new VPINews.Model.Admin();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getAdmin();
            //if (Session["ID"] == "1"&& Session["ID"] == "2")
            //{
            //    plList.Visible = true;
            //    plEdit.Visible = false;
            //    getAdmin();

            //}
            //else
            //{
            //    plList.Visible = false;
            //    plEdit.Visible = true;

            //}
        }

    }

    protected void getAdmin()
    {
        GridView1.DataSource = bll_admin.GetAllList();
        GridView1.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (labshow.Text == "修改管理员信息")
            {
                string u = (string)Session["User"];
                string UserName = this.txtUserName.Value;
                string PassWord = this.txtPWDOK.Value;
                if (UserName == u)
                {
                    smslabel.Text = "1号、2号管理员的账号禁止修改";
                }


                else
                {
                    mod_admin.UserName = UserName;
                    mod_admin.PassWord = PassWord;
                    bll_admin.Update(mod_admin);
                    smslabel.Text = "管理员修改成功";
                    getAdmin();
                    txtUserName.Value = "";

                    labshow.Text = "添加管理员信息";
                    btnAdd.Text = "· 添加 ·";
                }

            }

            else
            {
                string UserName = this.txtUserName.Value;
                string PassWord = this.txtPWDOK.Value;


                if (bll_admin.Exists(UserName))
                {
                    smslabel.Text = "管理员已存在，添加失败";
                    txtUserName.Value = "";

                }

                else
                {
                    mod_admin.UserName = UserName;
                    mod_admin.PassWord = PassWord;
                    bll_admin.Add(mod_admin);
                    smslabel.Text = "管理员添加成功";
                    getAdmin();
                    txtUserName.Value = "";
                }
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        smslabel.Text = "";
        txtUserName.Value = "";
        txtPWD.Value = "";
        txtPWDOK.Value = "";
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = bll_admin.GetAllList();
        GridView1.DataBind();

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteAdmin")
        {
            bll_admin.Delete(Convert.ToString(e.CommandArgument));
            getAdmin();

        }

        if (e.CommandName == "EditAdmin")
        {
            labshow.Text = "修改管理员信息";
            btnAdd.Text = "· 修改 ·";
            //id = Convert.ToInt32(this.GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);

            VPINews.BLL.Admin bll_admin = new VPINews.BLL.Admin();
            VPINews.Model.Admin mod_admin = bll_admin.GetModel(e.CommandArgument.ToString());
            this.txtUserName.Value = mod_admin.UserName;

            //this.txtUserName.Value = this.GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text.Trim();
            ////this.txtUserName.Value=this.GridView1.Rows[0].Cells[1].Text.Trim();
            //this.txtUserName.Value = e.CommandArgument.ToString();     

        }

    }

    protected void BindGridView(string items, string keyword)
    {
        DataSet ds = new DataSet();
        string sortExpression = string.Empty;
        if (ViewState["SortExpression"] != null)
        {
            sortExpression = ViewState["SortExpression"].ToString();
        }
        string sortDirection = string.Empty;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            sortDirection = "ASC";
            // GridViewSortDirection = SortDirection.Descending;
        }
        else
        {
            sortDirection = "DESC";
            // GridViewSortDirection =SortDirection .Ascending ;
        }

    }

 
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string clm = e.SortExpression;
        string sortdrt = e.SortDirection.ToString ();
        
        string sort =sortdrt==SortDirection .Ascending.ToString () ?"DESC" :"ASC";
        string sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {

            GridViewSortDirection = SortDirection.Descending;
        }

        else
        {
            GridViewSortDirection = SortDirection.Ascending;
        }
        ViewState["SortExpression"]= clm;   
        BindGridView(null , null );
    }

    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
            
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];

        }

        set { ViewState["sortDirection"] = value; }

    }



    

    /*
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"] == null)
        {
            ViewState["SortDirection"] = SortDirection.Descending;
        }
        for (int i = 0; i <= ((GridView)sender).Columns.Count - 1; i++)
        {
            ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText.Replace("▲", "");
            ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText.Replace("▼", "");
        }
        string ViewState_SortDirection = ViewState["SortDirection"].ToString();
        int Columns_i = 0;
        for (int i = 0; i <= ((GridView)sender).Columns.Count - 1; i++)
        {
            if (e.SortExpression == ((GridView)sender).Columns[i].SortExpression)
            {
                Columns_i = i;
                if (ViewState["SortDirection"].ToString() == SortDirection.Ascending.ToString())
                {
                    e.SortDirection = SortDirection.Descending;
                    ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + "▼";
                    ViewState["SortDirection"] = SortDirection.Descending;
                }
                else
                {
                    e.SortDirection = SortDirection.Ascending;
                    ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + "▲";
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
            }
        }
    }
     * */
    //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (GridView1.SortExpression.Length > 0)
    //    {
    //        int cellIndex = -1;
    //        foreach (DataControlField field in GridView1.Columns)
    //        {
    //            if (field.SortExpression == GridView1.SortExpression)
    //            {
    //                cellIndex = GridView1.Columns.IndexOf(field);
    //                break;
    //            }
    //        }

    //        if (cellIndex > -1)
    //        {
    //            if (e.Row.RowType == DataControlRowType.Header)
    //            {
    //                //  this is a header row,
    //                //  set the sort style
    //                e.Row.Cells[cellIndex].CssClass +=
    //                    (GridView1.SortDirection == SortDirection.Ascending
    //                    ? " sortascheader" : " sortdescheader");
    //            }
    //            else if (e.Row.RowType == DataControlRowType.DataRow)
    //            {
    //                //  this is an alternating row
    //                e.Row.Cells[cellIndex].CssClass +=
    //                    (e.Row.RowIndex % 2 == 0
    //                    ? " sortaltrow" : "sortrow");
    //            }
    //        }
    //    }
    //}
}